<div class="layui-fluid">
	<form class="layui-form" action="" lay-filter="goods-list-form" id="goods-list-form">
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" class="layui-input" id="test1" placeholder="选择时间范围">
			</div>
		</div>
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<input type="text" class="layui-input" id="test2" placeholder="选择时间范围">
			</div>
		</div>
		<div class="layui-form-item layui-inline">
			<div class="layui-input-inline">
				<select name="type" lay-verify="type" lay-filter="type" id="type">
					<option value="">请选择账户类型</option>
					<option value="0">全部用户</option>
					<option value="1">账户有余额用户</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item layui-input-inline" style="width:200px">
			<input type="text" name="keyword" id="keyword" placeholder="用户昵称" autocomplete="off" class="layui-input">
		</div>
		<div class="layui-form-item layui-inline" style="margin-left:20px;">
			<div class="layui-input-inline">
				<button class="layui-btn" lay-submit lay-filter="goods-list-search">搜索</button>
				<!-- <button class="layui-btn layui-btn-primary" lay-filter="goods-list-search1">重置</button>-->
			</div>
		</div>
	</form>

	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body">
					<!--<div class="layui-btn-group" style="margin-bottom: 10px;width: 100%;">
                <button class="layui-btn layuiadmin-btn-tags" style="float: right;" data-type="add">添加书单</button> 	
            </div>-->

					<table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
					<script type="text/html" id="wechat_avatar">
						<img src="{{d.wechat_avatar}}" alt="" style="width:82px; height: 82px;">
					</script>
					<script type="text/html" id="LAY-good-list-table-stock">
						<p>￥{{d.account}}</p>
					</script>
					<script type="text/html" id="test-table-operate-barDemo">
						<a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
					</script>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	layui.use(['admin', 'table', 'form', 'upload', 'laydate'], function() {
		var user_name_id = layui.router().search.user_name_id
		var table = layui.table,
			laydate = layui.laydate,
			form = layui.form,
			admin = layui.admin,
			upload = layui.upload,
			view = layui.view,
			element = layui.element,
			$ = layui.$,
			cols = [
				[{
					align: 'center',
					width: 130,
					templet: '#wechat_avatar',
					title: '头像',
					field: 'wechat_avatar'
				}, {
					align: 'center',
					field: 'wechat_nickname',
					title: '用户'
				}, {
					align: 'center',
					field: 'buy_orders_num',
					title: '买书订单次数',
					sort: true
				}, {
					align: 'center',
					field: 'sell_orders_num',
					title: '卖书订单次数',
					sort: true
				}, {
					align: 'center',
					field: 'account',
					title: '账户余额',
					sort: true,
					templet: '#LAY-good-list-table-stock'
				}, {
					align: 'center',
					field: 'recommend_users_count',
					sort: true,
					title: '邀请好友总数'
				}, {
					align: 'center',
					field: 'created_at',
					title: '创建时间'
				}, {
					align: 'center',
					fixed: 'right',
					title: '操作',
					toolbar: '#test-table-operate-barDemo'
				}]
			];
		laydate.render({
			elem: '#test1',
			type: 'datetime'
		});
		laydate.render({
			elem: '#test2',
			type: 'datetime'
		});
		table.set({
			headers: { //通过 request 头传递
				'access-token': layui.data('layuiAdmin').access_token
			}
		});
		table.render({
			elem: '#test-table-operate',
			url: 'https://bookshop.dingdangtoy.com/api/admin/user/index',
			parseData: function(res) { //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.message, //解析提示文本
					"data": res.data.items, //解析数据列表
					"count": res.data._meta.total_count //解析数据长度	
				}
			},
			cols: cols,
			page: true
		});
        form.render(null, 'goods-list-form');
		//监听工具条
		table.on('tool(test-table-operate)', function(obj) {
			var data = obj.data;
			if(obj.event === 'edit') {
				admin.popup({
					title: '查看用户',
					area: ['80%', '96%'],
					id: 'LAY-popup-content-tags',
					success: function(layero, index) {
						view(this.id).render('user-manage/all/add', data).done(function() {
							// 跳转收书列表
							$('#order_books1').click(function() {
								location.hash = '/buy-orders/all/index/user_id=' + data.id
								return false;
							})
							// 跳转卖书列表
							$('#order_books').click(function() {
								location.hash = '/sale-orders/all/index/user_id=' + data.id
								return false;
							})
							// 跳转订单退回列表
							$('#order_books2').click(function() {
								location.hash = '/return-order/order/index/user_id=' + data.id
								return false;
							})
							$.ajax({
								type: "GET",
								dataType: "json",
								url: "https://bookshop.dingdangtoy.com/api/admin/user/show?id=" + data.id,
								data: {},
								headers: { //通过 request 头传递
									'access-token': layui.data('layuiAdmin').access_token
								},
								success: function(data) {
									if(parseInt(data.code) == 0) {
										$('#wechat_avatar1').attr('src', data.data.wechat_avatar)
										$('#wechat_nickname').text(data.data.wechat_nickname)
										$('#account').text(data.data.account)
										$('#created_at').text(data.data.created_at)
										$('#recommend_users_success').text(data.data.recommend_users_success)
										$('#buy_orders_num').text(data.data.buy_orders_num)
										$('#buy_books_num').text(data.data.buy_books_num)
										$('#sell_orders_num').text(data.data.sell_orders_num)
										$('#sell_books_num1').text(data.data.sell_books_num1)
										$('#sell_books_num2').text(data.data.sell_books_num2)
										$('#back_orders_num').text(data.data.back_orders_num)
										$('#back_books_num').text(data.data.back_books_num)
										$('#get_money').text(data.data.get_money)
										if(parseInt(data.data.is_official_account) == 1) {
											$('#wechat_nickname1').text('已关注公众号')
										} else {
											$('#wechat_nickname1').text('未关注公众号')
										}
										$('#recommend_users_total').text(data.data.recommend_users_total)
										if(data.data.addresses.length > 0) {
											$('#base-address').show();
										}
										var _html = '';
										var _html1 = '';
										var _html2 = '';
										var num = 0;
										for(var i = 0; i < data.data.addresses.length; i++) {
											num += 1
											_html += '<li><p>地址信息' + num + '</p><p><span style="margin-right:20px">' + data.data.addresses[i].receiver + '</span>' + data.data.addresses[i].mobile + '</p><p>' + data.data.addresses[i].province_name + '' + data.data.addresses[i].city_name + '' + data.data.addresses[i].area_name + '' + data.data.addresses[i].address + '</p></li>';
										}
										$('#address-list').append(_html)
										for(var i = 0; i < data.data.account_records1.length; i++) {
											_html1 += '<tr><td>卖书（' + data.data.account_records1[i].books_num + '本）</td><td>' + data.data.account_records1[i].created_at + '</td><td>' + data.data.account_records1[i].platform_success_time + '</td><td>' + data.data.account_records1[i].actual_price + '</td></tr>';
										}
										$('#table1').append(_html1)
										for(var i = 0; i < data.data.account_records2.length; i++) {
											if(parseInt(data.data.account_records2[i].state) == 1) {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td>提现成功</td></tr>';
											} else {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td stle="color:red">提现失败</td></tr>';

											}
										}
										$('#table2').append(_html2)

										form.render();
									} else {
										layer.msg(data.message);
									}
								},
								error: function(msg) {
									console.log(msg)
								}
							});

						});
					}
				});
			}
		});
		// 搜索 ,

		form.on('submit(goods-list-search)', function(data) {
			table.reload('test-table-operate', {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					'start_time': $('#test1').val(),
					'end_time': $('#test2').val(),
					'type': $('#type option:selected').val(),
					'keyword': $('#keyword').val()
				}
			});
			return false;
		});

	});
</script>
<style type="text/css">
	.layui-table-cell, .layui-table-tool-panel li{white-space: inherit !important;}
</style>